package com.tom.angel.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.tom.angel.model.Menu;
import com.tom.angel.pagemodel.TreeMenuPageModel;
import com.tom.angel.service.MenuServiceI;
import com.tom.angel.util.Log4jUntil;

@SuppressWarnings("serial")
@ParentPackage("basePackage") 
@Namespace("/")
@Action(value="menuAction")
@Results( { @Result(name = "success", location = "/menu.jsp"), 
@Result(name = "error", location = "/error.jsp") }) 
public class MenuAction extends ActionSupport {
	
	//自动注入service
	private MenuServiceI menuService;

	public MenuServiceI getMenuService() {
		return menuService;
	}

	@Autowired
	public void setMenuService(MenuServiceI menuService) {
		this.menuService = menuService;
	}

	public void treeMenu(){
		List<TreeMenuPageModel> menus = new ArrayList<TreeMenuPageModel>();
		HttpServletRequest request =  ServletActionContext.getRequest();
		String id = request.getParameter("id");
		Menu menu = null;
		if(id != null && !"".equals(id)){
			menu = menuService.findMenu(Integer.parseInt(id));
		}
		boolean flag = false;
		menus = menuService.getMenu(menu,flag);
		
		try {
			ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(menus));
		} catch (IOException e) {
			Log4jUntil.outPutLog(ServletActionContext.class, e.getMessage());
		}
	}
}
